package com.hotel.front;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.POSUnitLogic;

import data.POSUnit;
import data.passingData.ResultData;
@SuppressWarnings("serial")
public class POSUnitService extends HttpServlet{
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("getAllPOSUnit")){
				getAllPOSUnit(req,resp);
			}else if(doClass.equals("addPOSUnit")){
				AddPOSUnit(req,resp);
			}else if(doClass.equals("delPOSUnit")){
				DeletePOSUnit(req,resp);
			}else if(doClass.equals("editPOSUnit")){
				EditPOSUnit(req,resp);
			}else if(doClass.equals("getPOSUnitById")){
				GetPOSUnitById(req,resp);
			}else if(doClass.equals("editStatusPOSUnitById")){
				ChangeStatus(req,resp);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	private void AddPOSUnit(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String symbol = req.getParameter("symbol");
		String title = req.getParameter("title");
		Object obj = new POSUnitLogic(req,resp).AddPOSUnit(hotelId,symbol,title);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void EditPOSUnit(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long unitId = Long.parseLong(req.getParameter("id"));
		String symbol = req.getParameter("symbol");
		String title = req.getParameter("title");
		Object obj = new POSUnitLogic(req,resp).EditPOSUnit(unitId,symbol,title);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long amenId = Long.parseLong(req.getParameter("unitId"));
		Object obj = new POSUnitLogic(req,resp).ChangeStatus(amenId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void DeletePOSUnit(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String unitIdList = req.getParameter("unitId");
		POSUnitLogic posUnitLogic = new POSUnitLogic(req,resp);
		for(int i = 0; i < unitIdList.split(",").length ; i++){
			posUnitLogic.DeletePOSUnit(Long.parseLong(unitIdList.split(",")[i]));
		}
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(true));
	}
	
	public void getAllPOSUnit(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		POSUnitLogic posUnitLogic = new POSUnitLogic(req,resp);
		Object obj = posUnitLogic.GetAllPOSUnitLimit(hotelId,offset,line);
		double dataSize = posUnitLogic.GetAllPOSUnit(hotelId).size();
		Gson gson = new Gson();
		List<Object> rsObj = Arrays.asList(dataSize,pointer,line,obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(rsObj));
	}

	private void GetPOSUnitById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		POSUnit obj = new POSUnitLogic(req,resp).GetPOSUnit(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}

}